home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / store / Concurrent.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.0 KB  |  110 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import generators
  5. from threading import Lock
  6.  
  7. class ResponsibleGenerator(object):
  8.     '''A generator that will help clean up when it is done being used.'''
  9.     __slots__ = [
  10.         'cleanup',
  11.         'gen']
  12.     
  13.     def __init__(self, gen, cleanup):
  14.         self.cleanup = cleanup
  15.         self.gen = gen
  16.  
  17.     
  18.     def __del__(self):
  19.         self.cleanup()
  20.  
  21.     
  22.     def __iter__(self):
  23.         return self
  24.  
  25.     
  26.     def next(self):
  27.         return self.gen.next()
  28.  
  29.  
  30.  
  31. class Concurrent(object):
  32.     
  33.     def __init__(self, store):
  34.         self.store = store
  35.         self._Concurrent__visit_count = 0
  36.         self._Concurrent__lock = Lock()
  37.         self._Concurrent__pending_removes = []
  38.         self._Concurrent__pending_adds = []
  39.  
  40.     
  41.     def add(self, .1):
  42.         (s, p, o) = .1
  43.         if self._Concurrent__visit_count == 0:
  44.             self.store.add((s, p, o))
  45.         else:
  46.             self._Concurrent__pending_adds.append((s, p, o))
  47.  
  48.     
  49.     def remove(self, .1):
  50.         (subject, predicate, object) = .1
  51.         if self._Concurrent__visit_count == 0:
  52.             self.store.remove((subject, predicate, object))
  53.         else:
  54.             self._Concurrent__pending_removes.append((subject, predicate, object))
  55.  
  56.     
  57.     def triples(self, .1):
  58.         (subject, predicate, object) = .1
  59.         g = self.store.triples((subject, predicate, object))
  60.         pending_removes = self._Concurrent__pending_removes
  61.         self._Concurrent__begin_read()
  62.         for s, p, o in ResponsibleGenerator(g, self._Concurrent__end_read):
  63.             if (s, p, o) not in pending_removes:
  64.                 yield (s, p, o)
  65.                 continue
  66.         
  67.         for s, p, o in self._Concurrent__pending_adds:
  68.             if subject == None or subject == s:
  69.                 if predicate == None or predicate == p:
  70.                     if object == None or object == o:
  71.                         yield (s, p, o)
  72.                         continue
  73.         
  74.  
  75.     
  76.     def __len__(self):
  77.         return self.store.__len__()
  78.  
  79.     
  80.     def __begin_read(self):
  81.         lock = self._Concurrent__lock
  82.         lock.acquire()
  83.         self._Concurrent__visit_count = self._Concurrent__visit_count + 1
  84.         lock.release()
  85.  
  86.     
  87.     def __end_read(self):
  88.         lock = self._Concurrent__lock
  89.         lock.acquire()
  90.         self._Concurrent__visit_count = self._Concurrent__visit_count - 1
  91.         if self._Concurrent__visit_count == 0:
  92.             pending_removes = self._Concurrent__pending_removes
  93.             while pending_removes:
  94.                 (s, p, o) = pending_removes.pop()
  95.                 
  96.                 try:
  97.                     self.store.remove((s, p, o))
  98.                 continue
  99.                 print s, p, o, 'Not in store to remove'
  100.                 continue
  101.  
  102.             pending_adds = self._Concurrent__pending_adds
  103.             while pending_adds:
  104.                 (s, p, o) = pending_adds.pop()
  105.                 self.store.add((s, p, o))
  106.         
  107.         lock.release()
  108.  
  109.  
  110.